﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics; // BasicEffect, Vertex*, GraphicsDevice
using Microsoft.Xna.Framework;  // Matrix
using Microsoft.Xna.Framework.Content;  //  Content Manager

namespace XNA3_Demos
{
    class clsPlane
    {
        private GraphicsDevice device;
        private float width, height;
        // Vertex buffer
        private VertexBuffer vertexBuffer;

        // Texture used to draw the plane
        private Texture2D _texture;

        private BasicEffect effect;

        public Matrix worldMatrix = Matrix.Identity;

        public clsPlane(GraphicsDevice graphicsDevice)
        {
            device = graphicsDevice;
        }

        public void UnloadContent()
        {
            if (vertexBuffer != null)
            {
                vertexBuffer.Dispose();
                vertexBuffer = null;
            }
            if (effect != null)
            {
                effect.Dispose();
                effect = null;
            }
        }

        public void LoadContent(Texture2D texture)
        {
            //  Load the texture
            _texture = texture;

            //  Create the Plane
          

            //  Create the effect that will be used to draw the plane
            effect = new BasicEffect(device);

            //  Calculates the effect aspect ratio, projection and view matrix
            float aspectRatio = (float)device.Viewport.Width / device.Viewport.Height;
            effect.View = Matrix.CreateLookAt(new Vector3(0.0f, 2.0f, 2.0f),
                                  Vector3.Zero,
                                  Vector3.Up); ;
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                               MathHelper.ToRadians(45.0f),
                               aspectRatio, 1.0f, 10.0f);


            //  Enable and inform texture
            effect.TextureEnabled = true;
            effect.Texture = _texture;

            //  Enable lighting and inform light details
            effect.LightingEnabled = false;
            //effect.EnableDefaultLighting();

            effect.DirectionalLight0.Enabled = true;
            effect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(1, -1, 0));
            effect.DirectionalLight0.DiffuseColor = new Vector3(1f, 1f, 1f);
            effect.AmbientLightColor = new Vector3(0.4f, 0.4f, 0.4f);
        }

        public void CreatePlane(int _width,int _height)
        {
            //  size of 3D Axis 
            float PlaneLenghtRatio = 0.50f;
            width = 2.0f;
            height = 2.0f;
            float repetition = 4f;
            //  Number of vertices we´ll use
            int vertexCount = 4;

            VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[vertexCount];
            // X axis
            vertices[0] = new VertexPositionNormalTexture(new Vector3(-width * .5f, 0.0f, -height * .5f), Vector3.Up, new Vector2(0f, repetition));
            vertices[1] = new VertexPositionNormalTexture(new Vector3(width * .5f, 0.0f, -height * .5f), Vector3.Up, new Vector2(repetition, repetition));
            vertices[2] = new VertexPositionNormalTexture(new Vector3(-width * .5f, 0.0f, height * .5f), Vector3.Up, new Vector2(0f, 0f));
            vertices[3] = new VertexPositionNormalTexture(new Vector3(width * .5f, 0.0f, height * .5f), Vector3.Up, new Vector2(repetition, 0f));
            //  fill the vertex buffer with the vertices
            vertexBuffer = new VertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, vertexCount,
                                                    BufferUsage.WriteOnly);
            vertexBuffer.SetData<VertexPositionNormalTexture>(vertices);
        }

        public float getWidth()
        {
            return width;
        }

        public float getHeight()
        {
            return height;
        }


        public void Draw()
        {
            //  Set the World Matrix
            effect.World = worldMatrix;

            device.SetVertexBuffer(vertexBuffer);
            
            //  Create a vertex declaration to be used when drawing the vertices
            //device.VertexDeclaration = new VertexDeclaration( VertexPositionNormalTexture.VertexDeclaration.GetVertexElements());
            //  Set the vertex source
            //device.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);

            //  Draw the plane
            //effect.Begin();
            foreach (EffectPass CurrentPass in effect.CurrentTechnique.Passes)
            {
                CurrentPass.Apply();
                device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);

                //CurrentPass.Begin();
                //  We are drawing 6 vertices, grouped in 2 triangles (in a triangle strip)
                //device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
                //CurrentPass.End();
            }
            //effect.End();
            
        }
    }
}
